﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： GovUserRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-01-09
// 负责人：lsc
// ===================================================================
using System.Text;
using CGT.Infrastructure.Entity;
using Dapper;
using Dapper.Extensions;

namespace CGT.Infrastructure.Repository
{
    /// <summary>
    ///用户 仓储层
    /// </summary>	
    public class GovUserRepository : BaseRepository<GovUser>
    {

        #region 构造

        public GovUserRepository() : base() { }

        public GovUserRepository(string connectionString) : base(connectionString) { }

        public GovUserRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        /// <summary>
        /// 根据登陆帐号获取用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public GovUser GetByUserName(string userName)
        {
            string sql = "SELECT  * FROM [GovUser] WHERE [UserName] = @UserName AND [IsActive] = 1";
            return FirstOrDefault(sql, new { UserName = userName });
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="sql">查询语句</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public Page<GovUser> GetPages(long pageIndex, long pageSize)
        {
            StringBuilder sql = new StringBuilder("SELECT * FROM GovUser WITH(NOLOCK) where IsActive =1");
            return Pages(pageIndex, pageSize, sql.ToString());
        }
        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="state">状态</param>
        /// <param name="realName">用户名</param>
        /// <param name="loginName">登录名</param>
        /// <returns></returns>
        public Page<GovUser> GetPages(long pageIndex, long pageSize, short state, string realName, string loginName)
        {
            StringBuilder sql = new StringBuilder("SELECT * FROM GovUser WITH(NOLOCK) where IsActive =1 AND State=@State AND  (CHARINDEX(@UserName,UserName) >0 OR @UserName='' OR @UserName=null) AND(CHARINDEX(@RealName, RealName) > 0 OR @RealName = '' OR @RealName = null)");
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@UserName", loginName, System.Data.DbType.String);
            dparams.Add("@RealName", realName, System.Data.DbType.String);
            dparams.Add("@State", state, System.Data.DbType.Int16);
            return Pages(pageIndex, pageSize, sql.ToString(), dparams);
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        public void BatchLogicDelete(string ids)
        {
            var sql = string.Format("UPDATE GovUser   SET IsActive=0 , UpdateTime = GETDATE()  WHERE UserID in ({0})", ids);
            Execute(sql);
        }
        /// <summary>
        ///  根据用户编码获取用户，排除当前用户
        /// </summary>
        /// <param name="userId">需要排除的用户ID</param>
        /// <param name="num">编号</param>
        /// <param name="isPolice">是否是警员</param>
        /// <returns></returns>
        public GovUser GetUserByNum(int userId, string num, short isPolice)
        {
            var strSql = "select * from GovUser with(nolock) WHERE UserNumber=@UserNumber and IsPolice=@IsPolice  and UserID != @UserID  ";
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@UserNumber", num, System.Data.DbType.String);
            dparams.Add("@IsPolice", isPolice, System.Data.DbType.Int16);
            dparams.Add("@UserID", userId, System.Data.DbType.Int32);
            return FirstOrDefault(strSql, dparams);
        }
        /// <summary>
        ///  根据用户编码获取用户
        /// </summary>
        /// <param name="num">编号</param>
        /// <param name="isPolice">是否是警员</param>
        /// <returns></returns>
        public GovUser GetUserByNum(string num, short isPolice)
        {
            var strSql = "select * from GovUser with(nolock) WHERE UserNumber=@UserNumber and IsPolice=@IsPolice ";
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@UserNumber", num, System.Data.DbType.String);
            dparams.Add("@IsPolice", isPolice, System.Data.DbType.Int16);
            return FirstOrDefault(strSql, dparams);
        }
        /// <summary>
        ///  根据登录帐号获取用户，排除当前用户
        /// </summary>
        /// <param name="userId">需要排除的用户ID</param>
        /// <param name="loginName">登录帐号</param>
        /// <returns></returns>
        public GovUser GetUserByLoginName(int userId, string loginName)
        {
            var strSql = "select * from GovUser with(nolock) WHERE UserName=@UserName  and UserID != @UserID  ";
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@UserName", loginName, System.Data.DbType.String);
            dparams.Add("@UserID", userId, System.Data.DbType.Int32);
            return FirstOrDefault(strSql, dparams);
        }
        /// <summary>
        ///  根据登录帐号获取用户
        /// </summary>
        /// <param name="loginName">登录帐号</param>
        /// <returns></returns>
        public GovUser GetUserByLoginName(string loginName)
        {
            var strSql = "select * from GovUser with(nolock) WHERE UserName=@UserName ";
            return FirstOrDefault(strSql, new { UserName = loginName });
        }

        /// <summary>
        ///  根据身份证号码获取用户，排除当前用户
        /// </summary>
        /// <param name="userId">需要排除的用户ID</param>
        /// <param name="IdentityCode">身份证号码</param>
        /// <returns></returns>
        public GovUser GetUserByIdentityCode(int userId, string IdentityCode)
        {
            var strSql = "select * from GovUser with(nolock) WHERE IdentityCode=@IdentityCode  and UserID != @UserID  ";
            return FirstOrDefault(strSql, new { IdentityCode = IdentityCode, UserID = userId });
        }
        /// <summary>
        ///  根据身份证号码获取用户，排除当前用户
        /// </summary>
        /// <param name="IdentityCode">身份证号码</param>
        /// <returns></returns>
        public GovUser GetUserByIdentityCode(string IdentityCode)
        {
            var strSql = "select * from GovUser with(nolock) WHERE IdentityCode=@IdentityCode ";
            DynamicParameters dparams = new DynamicParameters();
            return FirstOrDefault(strSql, new { IdentityCode = IdentityCode });
        }
    }
}